package iut.acy.retardeleve;

import java.util.ArrayList;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.ListView;

public class MainActivity extends Activity implements OnItemClickListener, OnClickListener{

	
	/*private static final String FLAG_SUCCESS = "success";

	private static final String FLAG_MESSAGE = "message";*/
	private static final String LOGIN_URL = "http://retard.hol.es/action.php";

	private ArrayList<Retard> retards = new ArrayList<Retard>();
	
	ListView lvListe;
	
	private Intent intent;
	
	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		Button updateB = (Button)findViewById(R.id.updateB);
		updateB.setOnClickListener(this);

		lvListe = (ListView)findViewById(R.id.listView);


		lvListe.setOnItemClickListener(this);
		
	}

	private void remplirListe()
	{
		CallbackFiller callback = new CallbackFiller(this, retards, lvListe);
		new ConnectTask().execute(LOGIN_URL, callback);
		RetardAdapter adapter = new RetardAdapter(this, retards);
		lvListe.setAdapter(adapter);
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) 
	{
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	public void onItemClick(AdapterView<?> parent, View view,int position, long id)
	{
		Retard retard = (Retard) ((RetardAdapter) lvListe.getAdapter()).getRetards().get(position);
		Detail.setRetardDetail(retard);
		if(Detail.getRetardDetail() != null)
			intent = new Intent(MainActivity.this, Detail.class);
			startActivity(intent);
	}

	@Override
	public void onClick(View v) {
		switch(v.getId()){
		case R.id.updateB : this.remplirListe();
		break;
		default:break;
		
		}
		
	}
	
}